const Controller = require("egg").Controller;
const { GenerateToken } = require("../utils/token");
const { success, fail } = require("../utils/code");

class UserController extends Controller {
  // 用户登录
  async login() {
    const { ctx } = this;
    const { name, password } = ctx.request.body;
    console.log('username, password', name, password);
    const token = GenerateToken(this, { name, password });

    const { type, message = "" } = await ctx.service.user.login(
      name,
      password,
      token
    );
    if (type === "success") {
      ctx.body = success({
        token,
      });
    } else {
      ctx.body = fail({}, message);
    }
  }

  // 用户退出
  async logout() {
    const { ctx } = this;
    let token = ctx.request.header.token;

    const result = await ctx.service.user.logout(token);

    if (result) {
      ctx.body = success();
    } else {
      ctx.body = fail();
    }
  }

  // 获取用户个人信息
  async info() {
    const { ctx } = this;
    const { id = "" } = ctx.request.query;
    let token = ctx.request.header.token;

    const result = await ctx.service.user.info(id, token);
    if (result) {
      ctx.body = success(result);
    } else {
      ctx.body = fail();
    }
  }

  // 添加用户
  async add() {
    const { ctx } = this;
    const { name, password } = ctx.request.body;
    const user = await ctx.service.user.info_by_name(name);
    if (user) {
      ctx.body = fail({}, "该用户已经存在");
    } else {
      const result = await ctx.service.user.add(name, password);
      if (result) {
        ctx.body = success();
      } else {
        ctx.body = fail();
      }
    }
  }

  // 编辑用户
  async edit() {
    const { ctx } = this;
    const { id, name } = ctx.request.body;
    const result = await ctx.service.user.edit(id, name);
    if (result) {
      ctx.body = success();
    } else {
      ctx.body = fail();
    }
  }

  // 查询用户列表
  async list() {
    const { ctx } = this;
    const query = ctx.request.query;
    const result = await ctx.service.user.list(query);
    if (result) {
      ctx.body = success(result);
    } else {
      ctx.body = fail();
    }
  }

  // 删除用户
  async delete() {
    const { ctx } = this;
    const body = ctx.request.body;
    const result = await ctx.service.user.delete(body);
    if (result) {
      ctx.body = success();
    } else {
      ctx.body = fail();
    }
  }

  // 用户分配角色
  async add_role() {
    const { ctx } = this;
    const body = ctx.request.body;
    const result = await ctx.service.user.add_role(body);
    if (result) {
      ctx.body = success();
    } else {
      ctx.body = fail();
    }
  }

  // 用户分配菜单
  async add_menu() {
    const { ctx } = this;
    const body = ctx.request.body;
    const result = await ctx.service.user.add_role(body);
    if (result) {
      ctx.body = success();
    } else {
      ctx.body = fail();
    }
  }

  // 验证token
  async testToken() {
    const { ctx } = this;
    const token = ctx.request.header.token;
    try {
      ctx.app.jwt.verify(token, ctx.app.jwt.secret);
      ctx.body = "token正常";
    } catch (err) {
      ctx.body = "token有问题";
    }
  }
}
module.exports = UserController;
